# -*- coding: utf-8 -*-

import os.path
import gtk
import gtk.gdk
from Simulinux import config
from Simulinux.Views.DiagramView import DiagramView
from Simulinux.Palette import Palette
from Simulinux.Commands.CommandsHistory import CommandsHistory

CUSTOM_ICONS = {
    'simulinux-select': 'select.png', 
    'simulinux-text': 'text.png', 
    'simulinux-net': 'net.png'
}

class GUI:
    def __init__(self):
        self.__builder = gtk.Builder()
        self.__builder.add_from_file(config.GTKBUILDER_FILE)
        self.__cursors = {
            'normal': gtk.gdk.Cursor(gtk.gdk.LEFT_PTR),
            'crosshair': gtk.gdk.Cursor(gtk.gdk.CROSSHAIR),
            'text-edit': gtk.gdk.Cursor(gtk.gdk.XTERM)
        }
        self.__palette = Palette(self.__builder)
        self.__drawing = self.__builder.get_object("model_diagram")
        self.__viewDiagram = DiagramView(self, self.__drawing)
        self.__history = CommandsHistory()

    def get_builder(self):
        return self.__builder

    def config(self):
        self.__config_icons()
        #Getting useful widgets
        self.__notebook = self.__builder.get_object("notebook_documents")
        #Getting main __window
        self.__window = self.__builder.get_object("window1")
        self.__window.connect("destroy", self.__quit)
        #Setting icon and title
        iconpath = os.path.join(config.ICONS_DIR, "simulinux.png")
        self.__window.set_icon_from_file(iconpath)
        self.__window.set_title("Simulinux")
        #Events
        dicionario_handlers = {
           "diagram_expose": self.__viewDiagram.expose,
           "diagram_button_press": self.__viewDiagram.button_pressed,
           "diagram_button_release": self.__viewDiagram.button_released,
           "diagram_motion_notify": self.__viewDiagram.mouse_moved,
           "diagram_key_press": self.__viewDiagram.key_pressed,
           "diagram_drag_data_received": self.__viewDiagram.drag_data_received,
           "treeview_drag_data_get": self.__palette.drag_data_get,
           "library_changed": self.__palette.library_changed,
           "block_selected": self.__palette.block_selected
        }
        self.__builder.connect_signals(dicionario_handlers)
   
    def show(self):
        self.__window.show()
        gtk.main()
    
    #Command related
    
    def execute_command(self, command):
        self.__history.addCommand(command)
        command.execute()
    
    def undo(self):
        self.__history.undo()
    
    def redo(self):
        self.__history.redo()
    
    #Services
    
    def change_cursor(self, cursor):
        window = self.__drawing.get_root_window()
        window.set_cursor(self.__cursors[cursor])
    
    def get_key_string(self, event):
        key = ''
        if (event.state & gtk.gdk.CONTROL_MASK): key += '<CTRL>'
        if (event.state & gtk.gdk.META_MASK): key += '<ALT>'
        if (event.state & gtk.gdk.SHIFT_MASK): key += '<SHIFT>'
        key += gtk.gdk.keyval_name(event.keyval)
        return key
    
    #Private methods
    def __quit(self, widget, *event):
        gtk.main_quit()
        
    def __config_icons(self):
        #@FIXME
        self.__factory = self.__builder.get_object("iconfactory1")
        for name,file in CUSTOM_ICONS.iteritems():
            iconpath = os.path.abspath(os.path.join(config.ICONS_DIR, file))
            source = gtk.IconSource()
            set = gtk.IconSet()
            source.set_filename(iconpath)
            set.add_source(source)
            self.__factory.add(name, set)
        self.__factory.add_default()
        
